﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.Specialized;
using Ample.Model;

namespace Ample.BLL.Store
{
    [System.ComponentModel.DataObject]
    public class OrderBO
    {
        #region Get Methods

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
        public IEnumerable<Order> GetOrders()
        {
            return DAL.OrderManager.Create().GetOrders();
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public Order GetOrderById(int orderId)
        {
            return DAL.OrderManager.Create().GetOrderById(orderId);
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public Order GetOrderByNo(string orderNo)
        {
            return DAL.OrderManager.Create().GetOrderByNo(orderNo);
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public IEnumerable<Order> GetOrderByUserName(string username)
        {
            return DAL.OrderManager.Create().GetOrderByUsername(username);
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public IEnumerable<Order> GetOrdersRecentStatusByUsername(string username)
        {
            return DAL.OrderManager.Create().GetOrdersRecentStatusByUsername(username);
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public IEnumerable<Order> GetOrdersRecentStatus(string status, DateTime from, DateTime to)
        {
            return DAL.OrderManager.Create().GetOrdersRecentStatus( status, from, to);
        }

        public static StringCollection GetOrderStatuses()
        {
            return DAL.OrderManager.GetOrderStatuses(true);
        }
        

        #endregion


        #region Insert Methods

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int InsertOrder(Order order)
        {
            order.OrderNo = GenerateOrderNo();
            OperationStatus opStatus = DAL.OrderManager.Create().InsertOrder(order);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.OrderRules.InsertOrder: {0}", opStatus.Exception.Message));
            }

            return opStatus.InsertedRowID;
        }

        #endregion


        #region Update Methods

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update, true)]
        public void UpdateOrder(Order order)
        {
            //ApplyRules(prod);
            OperationStatus opStatus = DAL.OrderManager.Create().UpdateOrder(order);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.OrderRules.UpdateOrder: {0}", opStatus.Exception.Message));
            }
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update, false)]
        public void UpdateOrderImplicit(Order order)
        {
            //ApplyRules(prod);
            OperationStatus opStatus = DAL.OrderManager.Create().UpdateOrderImplicit(order);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.OrderRules.UpdateOrder: {0}", opStatus.Exception.Message));
            }
        }


       #endregion


        #region Delete Methods

        public static void DeleteOrder(Order prod)
        {
            OperationStatus opStatus = DAL.OrderManager.Create().DeleteOrder(prod);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.OrderRules.DeleteOrder: {0}", opStatus.Exception.Message));
            }
        }

        public static void DeleteOrder(int Id)
        {
            OperationStatus opStatus = DAL.OrderManager.Create().DeleteOrder(Id);
            if (!opStatus.Status)
            {
                throw new ApplicationException(String.Format("Error occurred in BLL.OrderRules.DeleteOrder: {0}", opStatus.Exception.Message));
            }
        }

        #endregion

        #region Business Rules

        private static void ApplyRules(Order prod)
        {

        }

        public string GenerateOrderNo()
        {
           string orderNo = DAL.OrderManager.Create().GenerateOrderNo();
           return orderNo;
        }

        #endregion

    }
}
